﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using Extreme.SPFramework.DependencyInjection;
using Extreme.SPFramework.DomainModel;
using Extreme.SPFramework.Test.Entities;
using Extreme.SPFramework.Test.Interfaces;

namespace Extreme.SPFramework.Test.Repositories
{
    /// <summary>
    /// Test case linq repository
    /// </summary>
    [Service]
    public class CaseLinqRepository : LinqRepositoryBase<Case, DbTestDataContext>, ICaseRepository
    {
        public override System.Data.Linq.DataLoadOptions GetDefaultDataLoadOptions()
        {
            var options = new DataLoadOptions();
            options.LoadWith<Case>(c => c.Insurer);
            return options;
        }

        public List<Case> SearchByCriteria(CaseSearchCriteria critera)
        {
            if (critera == null)
                throw new ArgumentNullException("critera");

            List<Case> returnValue = null;
            using (var context = GetNewContext())
            {
                var querable = base.GetAllAsQuerable(context);
                if (!string.IsNullOrEmpty(critera.CaseName))
                    querable = querable.Where(item => item.Name == critera.CaseName);
                if (!string.IsNullOrEmpty(critera.InsurerFirstName))
                    querable = querable.Where(item => item.Insurer.Name == critera.InsurerFirstName);
                returnValue = querable.ToList();
                returnValue.ForEach(item => item.Detach());
            }

            return returnValue;
        }
    }
}
